﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.课堂练习2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Panel2.Visible = false;
            //是否第一次上这个页面
            if (Session["FirstVisit"] == null)
            {
                Label1.Visible = true;
                Session["FirstVisit"] = "no";
            }
            else
            {
                Label1.Visible = false;
            }
            //是否已经登录
            if(Session["User"] != null)
            {
                Panel1.Visible = false;
                int times = Convert.ToInt32(Session["times"].ToString());
                times++;
                Session["times"] = times;
                showpanle2();
                Session["lasttime"] = DateTime.Now.ToString();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //不显示登录框框了 ， 显示登出按钮，显示lable2登录成功
            Panel1.Visible = false;
            Label2.Visible = true;

            Session["User"] = TextBox1.Text;
            Session["lasttime"] = DateTime.Now.ToString();
            if (Session["times"] != null)
            {
                int times = Convert.ToInt32(Session["times"].ToString());
                times++;
                Session["times"] = times;
            }
            else
            {
                Session["times"] = 1;
            }

            showpanle2();
        }

        private void showpanle2()
        {
            //显示panel2并且从session获得欣喜
            Panel2.Visible = true;

            Label3.Text = Session["User"].ToString() + " 你好，欢迎再次光临！";
            Label4.Text = "这是你第 " + Session["times"].ToString() + " 访问本站 ";
            Label5.Text = "您上次登录时间是: " + Session["lasttime"].ToString();
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            Session["User"] = null;
            Session["times"] = null;
            Response.Redirect("WebForm1.aspx");
        }
    }
}